package com.kkb.service.impl;

import com.kkb.domain.AjaxResult;
import com.kkb.mapper.TbTemplateMapper;
import com.kkb.pojo.TbBusiness;
import com.kkb.pojo.TbBusinessExample;
import com.kkb.pojo.TbTemplate;
import com.kkb.pojo.TbTemplateExample;
import com.kkb.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 模型列表实现类
 **/
@Service
public class TemplateServiceImpl implements TemplateService {

    @Resource
    private TbTemplateMapper tbTemplateMapper;

    @Override
    public List<TbTemplate> getTemplate() {
        TbTemplateExample example = new TbTemplateExample();
        List<TbTemplate> tbTemplates = tbTemplateMapper.selectByExample(example);
        return tbTemplates;
    }

    @Override
    public TbTemplate selectTempById(Integer templateId) {
        return tbTemplateMapper.selectByPrimaryKey(templateId);
    }

    @Override
    public int insertTemplate(TbTemplate template) {
        template.setCreateTime(new Date());
        return tbTemplateMapper.insert(template);
    }


    @Override
    public boolean updateTempByPrimaryKey(TbTemplate tbTemplate) {
        int result = tbTemplateMapper.updateByPrimaryKey(tbTemplate);
        return result > 0 ? true : false;
    }

    @Override
    public AjaxResult removeTemplateById(Integer templateId) {
        int result = tbTemplateMapper.deleteByPrimaryKey(templateId);
        if(result>0){
            return AjaxResult.success();
        }
        return AjaxResult.error("删除模板失败！");
    }

    @Override
    public boolean queryNameIsExist(String categoryName) {
        TbTemplateExample example = new TbTemplateExample();
        TbTemplateExample.Criteria criteria = example.createCriteria();
        criteria.andTemplateNameEqualTo(categoryName);
        List<TbTemplate> tbTemplates = tbTemplateMapper.selectByExample(example);
        if(tbTemplates==null||tbTemplates.size()<1){
            return false;
        }
        return true;
    }

    @Override
    public TbTemplate getTemplateByCategoryId(int categoryId) {
        TbTemplate template = tbTemplateMapper.getTemplateByCategoryId(categoryId);
        return template;
    }
}
